//
//  HMOuthModel.swift
//  TJHWeiBo
//
//  Created by tianjunhui on 16/8/10.
//  Copyright © 2016年 itcast.cn. All rights reserved.
//

import UIKit
//使用归档和接档需要使用NSCoding
class HMOuthModel: NSObject,NSCoding {

    // 用户头像
    var avatar_large: String?
    
    //用户昵称
    var screen_name: String?
    
    //用户授权的唯一票据
    var access_token: String?
    //生命周期，单位是秒数
    var expires_in : NSTimeInterval = 0{
        didSet {
            expires_Date = NSDate(timeIntervalSinceNow: expires_in)
        }
        
    }
    //过期时间
    var expires_Date: NSDate?
    //授权用户的UID
    var uid : String?
    
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        // 用户头像
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        //用户昵称
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        //用户授权的唯一票据
        aCoder.encodeObject(access_token, forKey: "access_token")
        //过期时间
        aCoder.encodeObject(expires_Date, forKey: "expires_Date")
        //授权用户的UID
        aCoder.encodeObject(uid, forKey: "uid")
    }
    //解档
    required init?(coder aDecoder: NSCoder) {
        
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_Date = aDecoder.decodeObjectForKey("expires_Date") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        
    }
    
    
    
    
    
    init(dict:[String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    //重写description
    override var description: String{
        
        let keys = ["access_token","expires_Date","uid","screen_name","avatar_large"]
        return dictionaryWithValuesForKeys(keys).description
    }
    

}
